#!/usr/bin/python
# -*- coding: utf-8 -*-


import sys
from PyQt4 import QtGui, Qt, QtCore
import dbus, commands, pyosd, time, sys

class wifiblueShow(QtGui.QWidget):
	def __init__(self, parent=None):

		if self.checkProcess():
		  self.getAndKillProcess()

		QtGui.QWidget.__init__(self, parent)
		
		self.imageName = "/usr/local/bin/w_on.png"
		
		
		self.bus = dbus.SystemBus()
		
		COLOR = "#e125d6"
		FONT = "-*-helvetica-*-r-normal--35-*-*-*-*-*-*-*"
		self.p = pyosd.osd(font=FONT, colour=COLOR)
		self.p.set_pos(pyosd.POS_BOT)
		self.p.set_align(pyosd.ALIGN_RIGHT)
		self.p.set_vertical_offset(10)
		
		self.wirelessStatus = self.checkWirelessStatus()
		
		if self.wirelessStatus:
			self.imageName = "/usr/local/bin/w_off.png"
		if not self.wirelessStatus:
			self.imageName = "/usr/local/bin/w_on.png"
        
		pixmap = QtGui.QPixmap(self.imageName)
	        width = pixmap.width()
	        height = pixmap.height()

	        self.resize(width, height)
	        
	        self.setWindowFlags(QtCore.Qt.FramelessWindowHint | QtCore.Qt.WindowStaysOnTopHint)
	        
	        self.label = QtGui.QLabel(self)
        	self.label.setPixmap(QtGui.QPixmap(self.imageName))
        	self.setMask(pixmap.mask())

		self.label.setWindowFlags(QtCore.Qt.WindowStaysOnTopHint)

	        self.showCenter()

		self.changePic()
		self.timer = QtCore.QTimer()
		self.connect(self.timer, QtCore.SIGNAL("timeout()"), self.doAction)
		self.timer.setSingleShot(True)
		self.timer.start(500)

		self.timerclose = QtCore.QTimer()
		self.connect(self.timerclose, QtCore.SIGNAL("timeout()"), self.doClose)
		self.timerclose.setSingleShot(True)
		self.timerclose.start(1500)

	def doClose(self):
		sys.exit(1)
		self.close()

	def checkProcess(self):
	
	  output=commands.getoutput('ps aux | grep vestel-osdwifi-v1.py | sed "/$grep vestel-osdwifi-v1.py/d"')
	  if output: return True
	  else: return False
	  
	def getAndKillProcess(self):
	  output=commands.getoutput("ps aux | grep vestel-osdwifi-v1.py | awk '{print $2}'")
	  li=output.split("\n")
	  for pid in li:
	    s="kill "+pid
	    commands.getstatus(s)        
	        
	def keyPressEvent(self, event):
	
		try:
			self.timer.stop()
		except:
			pass
		if event.key() == QtCore.Qt.Key_Escape:
			sys.exit(0)
			self.close()
			
			
	def doAction(self):
		if self.wirelessStatus:
			status1 = commands.getstatusoutput("dbus-send --system --type=method_call --dest=org.freedesktop.NetworkManager /org/freedesktop/NetworkManager org.freedesktop.NetworkManager.setWirelessEnabled boolean:false")
			if status1[0] == 0:
				#s=r"беспроводное подключение недоступно"
				s=r"wireless is off"
				d=s.decode("utf-8")
				self.p.display(d.encode("utf-8"))

		elif not self.wirelessStatus:
			status1 = commands.getstatusoutput("dbus-send --system --type=method_call --dest=org.freedesktop.NetworkManager /org/freedesktop/NetworkManager org.freedesktop.NetworkManager.setWirelessEnabled boolean:true")
			if status1[0] == 0:
				#s=r"беспроводное подключение доступно"
				s=r"wireless is on"
				d=s.decode("utf-8")
				self.p.display(d.encode("utf-8"))

		else:
			pass
			
		try:
			self.timer.stop()
		except:
			pass

	
	def changePic(self):
	
		if self.wirelessStatus:
			self.imageName = "/usr/local/bin/w_off.png"
			
		elif not self.wirelessStatus:
			self.imageName = "/usr/local/bin/w_on.png"
			
		else:
			pass
		
		self.label.clear()
		self.label.setPixmap(QtGui.QPixmap(self.imageName))
		pixmap = QtGui.QPixmap(self.imageName)
		self.setMask(pixmap.mask())
		self.update()
	
	def checkWirelessStatus(self):
		wlan0 = self.bus.get_object('org.freedesktop.NetworkManager','/org/freedesktop/NetworkManager/Devices/wlan0')
		status = wlan0.getProperties(dbus_interface='org.freedesktop.NetworkManager.Devices')[4]
		if status: return True
		else: return False
		
	def showCenter(self):
	
	        screen = QtGui.QDesktopWidget().screenGeometry()
	        size =  self.geometry()
	        self.move((screen.width()-size.width())/2, (screen.height()-size.height()))
	        
if __name__ == "__main__":

	app = QtGui.QApplication(sys.argv)
	osd = wifiblueShow()
	
	osd.show()
	
	app.exec_()
